package com.bank.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 银行卡信息实体类
 * 对应数据库表：card_info
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2025-06-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("card_info")
public class CardInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 卡号-16位数字主键
     */
    @TableId(value = "card_id", type = IdType.INPUT)
    @NotBlank(message = "卡号不能为空")
    @Pattern(regexp = "^[0-9]{16}$", message = "卡号格式不正确，应为16位数字")
    private String cardId;

    /**
     * 客户编号-外键
     */
    @TableField("customer_id")
    @NotNull(message = "客户编号不能为空")
    private Integer customerId;

    /**
     * 存款类型编号-外键
     */
    @TableField("saving_id")
    @NotNull(message = "存款类型编号不能为空")
    private Integer savingId;

    /**
     * 币种-默认人民币
     */
    @TableField("cur_id")
    private String curId = "RMB";

    /**
     * 开卡日期-默认当日
     */
    @TableField("open_date")
    private LocalDate openDate;

    /**
     * 开卡金额-不少于1元
     */
    @TableField("open_money")
    @NotNull(message = "开卡金额不能为空")
    @DecimalMin(value = "1.00", message = "开卡金额不能少于1元")
    @Digits(integer = 13, fraction = 2, message = "开卡金额格式不正确")
    private BigDecimal openMoney;

    /**
     * 账户余额-不能为负数
     */
    @TableField("balance")
    @NotNull(message = "账户余额不能为空")
    @DecimalMin(value = "0.00", message = "账户余额不能为负数")
    @Digits(integer = 13, fraction = 2, message = "账户余额格式不正确")
    private BigDecimal balance;

    /**
     * 取款密码-6位数字默认888888
     */
    @TableField("password")
    @Pattern(regexp = "^[0-9]{6}$", message = "取款密码格式不正确，应为6位数字")
    private String password = "888888";

    /**
     * 是否挂失-默认否
     */
    @TableField("is_report_loss")
    private Boolean isReportLoss = false;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    /**
     * 客户信息（关联查询时使用）
     */
    @TableField(exist = false)
    private UserInfo userInfo;

    /**
     * 存款类型信息（关联查询时使用）
     */
    @TableField(exist = false)
    private DepositType depositType;

    /**
     * 构造函数
     */
    public CardInfo() {
    }

    /**
     * 带参构造函数
     * 
     * @param cardId 卡号
     * @param customerId 客户编号
     * @param savingId 存款类型编号
     * @param openMoney 开卡金额
     * @param password 取款密码
     */
    public CardInfo(String cardId, Integer customerId, Integer savingId, BigDecimal openMoney, String password) {
        this.cardId = cardId;
        this.customerId = customerId;
        this.savingId = savingId;
        this.openMoney = openMoney;
        this.balance = openMoney; // 初始余额等于开卡金额
        this.password = password;
        this.openDate = LocalDate.now();
    }

    /**
     * 获取挂失状态文本
     * 
     * @return 挂失状态文本
     */
    public String getReportLossText() {
        return isReportLoss != null && isReportLoss ? "是" : "否";
    }

    /**
     * 重写toString方法
     */
    @Override
    public String toString() {
        return "CardInfo{" +
                "cardId='" + cardId + '\'' +
                ", customerId=" + customerId +
                ", savingId=" + savingId +
                ", curId='" + curId + '\'' +
                ", openDate=" + openDate +
                ", openMoney=" + openMoney +
                ", balance=" + balance +
                ", password='" + password + '\'' +
                ", isReportLoss=" + isReportLoss +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                '}';
    }
}
